package psn.zjk.exporter;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;

import java.awt.*;

/**
 * @author sdyt_zhangjunkai
 * @version 1.0
 * @CLASS_NAME CellStyleBuilder
 * @DESCRIPTION
 * @CREATE_TIME 2020/9/10 9:12
 */
public class CellStyleBuilder {
    private ExcelSheetBuilder excelSheetBuilder;
    private XSSFCellStyle cellStyle;
    private XSSFWorkbook workbook;

    protected CellStyleBuilder(ExcelSheetBuilder excelSheetBuilder) {
        this.excelSheetBuilder = excelSheetBuilder;
        workbook = excelSheetBuilder.getWorkbook();
        cellStyle = workbook.getStylesSource().createCellStyle();
    }

    public CellStyleBuilder addBorder() {
        cellStyle.setBorderColor(XSSFCellBorder.BorderSide.LEFT, new XSSFColor(Color.BLACK));
        cellStyle.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, new XSSFColor(Color.BLACK));
        cellStyle.setBorderColor(XSSFCellBorder.BorderSide.TOP, new XSSFColor(Color.BLACK));
        cellStyle.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, new XSSFColor(Color.BLACK));
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        return this;
    }

    public CellStyleBuilder setBackGroundColor(Color color) {
        XSSFColor xssfColor = new XSSFColor(color);
        cellStyle.setFillForegroundColor(xssfColor);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return this;
    }

    public CellStyleBuilder setAlignment(HorizontalAlignment alignment) {
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
        return this;
    }

    public CellStyleBuilder setVerticalAlignment(VerticalAlignment alignment) {
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        return this;
    }

//    public CellStyleBuilder addDiagonal(XSSFCell cell) {
//        StylesTable stylesSource = workbook.getStylesSource();
//        XSSFCellBorder xssfCellBorder = new XSSFCellBorder();
//        CTBorder ctBorder = xssfCellBorder.getCTBorder();
//        CTBorderPr ctBorderPr = ctBorder.addNewDiagonal();
//        ctBorderPr.setStyle(STBorderStyle.Enum.forInt(2));
//        ctBorderPr.setColor(new XSSFColor(Color.BLACK).getCTColor());
//        int i = stylesSource.putBorder(xssfCellBorder);
//        return this;
//    }

    public XSSFFont createFont() {
        return workbook.createFont();
    }

    public CellStyleBuilder setFont(XSSFFont font) {
        cellStyle.setFont(font);
        return this;
    }

    public XSSFCellStyle build() {
        return cellStyle;
    }

}
